﻿using System;
using System.Data;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Source.Common;


namespace  Source.Web.FrameWork
{
    public interface IUtilityHelper
    {
        string GetDateFormat();
        string GetDateTimeFormat();
        string ConvertDecimalToDisplayFormat(decimal value);
        string ConvertDecimalToExchangeRateDisplayFormat(decimal value);
        string ConvertDateToDisplayValue(DateTime? dateTime);
        NumberFormatInfo GetNumberFormatInfo();
        string ConvertDateToDisplayValueIncludingTime(DateTime? dateTime);
        string GetLimitedText(string text, int numberOfCharachtersToKeep, string postfix);

    }

    public class UtilityHelper : IUtilityHelper
    {

        private readonly IClientSideUserManager userManager;

        public UtilityHelper(IClientSideUserManager userManager)
        {
            this.userManager = userManager;
        }

        public string GetDateFormat()
        {
            return userManager.GetCurrentUserInfo().GetDateFormatOrFallBackToDefault();
        }

        public string GetDateTimeFormat()
        {
            return  GetDateFormat() + " HH:mm";
        }

        public string ConvertDecimalToDisplayFormat(decimal value)
        {
            return value.ToString(GlobalConstants.decimalDisplayFormat, CultureInfo.InvariantCulture);
        }

        public string ConvertDecimalToExchangeRateDisplayFormat(decimal value)
        {
            return value.ToString(GetNumberFormatInfo());
        }

        public string ConvertDateToDisplayValue(DateTime? dateTime)
        {
            if (!dateTime.HasValue)
            {
                return "";
            }

            return dateTime.Value.ToString(GetDateFormat());
        }

        public NumberFormatInfo GetNumberFormatInfo()
        {
            NumberFormatInfo formatter = new NumberFormatInfo();
            formatter.NumberDecimalSeparator = GlobalConstants.decimalInputDecimalSeparator;
            formatter.NumberGroupSeparator = GlobalConstants.decimalInputGroupSeparator;
            return formatter;
        }

        public string ConvertDateToDisplayValueIncludingTime(DateTime? dateTime)
        {
            if (!dateTime.HasValue)
            {
                return "";
            }

            return dateTime.Value.ToString(GetDateTimeFormat());
        }

        public string GetLimitedText(string text, int numberOfCharachtersToKeep, string postfix)
        {
            if (!string.IsNullOrEmpty(text) && text.Length > numberOfCharachtersToKeep)
            {
                return text.Substring(0, numberOfCharachtersToKeep) + postfix;
            }

            return text;
        }
    }
}
